package com.witmore.neutron.ai.web.apiv2.controller;

import com.jk.common.response.ResponseResult;
import com.witmore.neutron.ai.web.annotation.RequiresPermission;
import com.witmore.neutron.ai.web.controller.bo.PluginBindRequest;
import com.witmore.neutron.ai.web.controller.vo.PluginGroupVO;
import com.witmore.neutron.ai.web.service.PluginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 插件组控制器
 *
 * @author 豆子高
 * @since 2025-01-16
 */
@Slf4j
@RequestMapping("/v2/chat/plugin-group")
@RestController
public class PluginControllerV2 {

    @Autowired
    private PluginService pluginService;

    /**
     * 获取所有可用插件组列表
     */
    @RequiresPermission("plugin:list")
    @GetMapping("/list")
    public ResponseResult<List<PluginGroupVO>> getAvailablePluginGroups(@RequestParam("agentId") Integer agentId) {
        try {
            List<PluginGroupVO> pluginGroups = pluginService.getAvailablePluginGroups(agentId);
            return ResponseResult.success(pluginGroups);
        } catch (Exception e) {
            log.error("获取插件组列表失败", e);
            return ResponseResult.fail(500, "获取插件组列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取智能体已绑定的插件组列表
     */
    @RequiresPermission("plugin:agent")
    @GetMapping("/agent/{agentId}")
    public ResponseResult<List<PluginGroupVO>> getAgentPluginGroups(@PathVariable Integer agentId) {
        try {
            List<PluginGroupVO> pluginGroups = pluginService.getAgentPluginGroups(agentId);
            return ResponseResult.success(pluginGroups);
        } catch (Exception e) {
            log.error("获取智能体插件组列表失败", e);
            return ResponseResult.fail(500, "获取智能体插件组列表失败: " + e.getMessage());
        }
    }

    /**
     * 绑定插件组到智能体
     */
    @RequiresPermission("plugin:bind")
    @PostMapping("/bind")
    public ResponseResult<String> bindPluginGroup(@RequestBody PluginBindRequest request) {
        try {
            boolean success = pluginService.bindPluginGroup(request);
            if (success) {
                return ResponseResult.success("插件组绑定成功");
            } else {
                return ResponseResult.fail(400, "插件组绑定失败");
            }
        } catch (Exception e) {
            log.error("插件组绑定失败", e);
            return ResponseResult.fail(500, "插件组绑定失败: " + e.getMessage());
        }
    }

    /**
     * 解绑插件组
     */
    @RequiresPermission("plugin:unbind")
    @DeleteMapping("/unbind/{agentId}/{pluginGroupId}")
    public ResponseResult<String> unbindPluginGroup(@PathVariable Integer agentId, @PathVariable Integer pluginGroupId) {
        try {
            boolean success = pluginService.unbindPluginGroup(agentId, pluginGroupId);
            if (success) {
                return ResponseResult.success("插件组解绑成功");
            } else {
                return ResponseResult.fail(400, "插件组解绑失败");
            }
        } catch (Exception e) {
            log.error("插件组解绑失败", e);
            return ResponseResult.fail(500, "插件组解绑失败: " + e.getMessage());
        }
    }

    /**
     * 更新插件组绑定配置
     */
    @RequiresPermission("plugin:update")
    @PutMapping("/update")
    public ResponseResult<String> updatePluginGroupBinding(@RequestBody PluginBindRequest request) {
        try {
            boolean success = pluginService.updatePluginGroupBinding(request);
            if (success) {
                return ResponseResult.success("插件组配置更新成功");
            } else {
                return ResponseResult.fail(400, "插件组配置更新失败");
            }
        } catch (Exception e) {
            log.error("插件组配置更新失败", e);
            return ResponseResult.fail(500, "插件组配置更新失败: " + e.getMessage());
        }
    }
}
